
export class LoadingTo {
    private preProg = 0
    private callProg?: (n: number) => void
    private callEnd?: () => void
    private awaitTime = 5
    private addProg = 3
    constructor(callEnd?: () => void, callProg?: (n: number) => void, awaitTime = 5, addProg = 3) {
        this.callEnd = callEnd
        this.callProg = callProg
        this.awaitTime = awaitTime
        this.addProg = addProg
        this.loading()
    }

    setPreProg(preProg = 100) {
        this.preProg = preProg
    }

    private async loading() {
        const callProg = this.callProg
        const awaitTime = this.awaitTime, addProg = this.addProg
        let v = 0
        while (v < 110) {
            await window.awaitTime(awaitTime)
            if (v < this.preProg) {
                v += addProg
                callProg?.(v)
            }
            if (v > 99.99) {
                this.callEnd?.()
                break
            }
        }
    }
}

